//  Gmof - A meta-object facility library based on OMG's EMOF 
//
//  Copyright (C) 2007 Peter Hagg <peter.hagg@iki.fi>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
#include <cppunit/TestAssert.h>
#include <cppunit/Test.h>
#include <cppunit/TestSuite.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/HelperMacros.h>

#include "../src/gmof.h"

class ReflectiveSequenceTest : public CppUnit::TestFixture 
{
	CPPUNIT_TEST_SUITE(ReflectiveSequenceTest);
	
	CPPUNIT_TEST(test_creation);
	CPPUNIT_TEST(test_add);
	CPPUNIT_TEST(test_get);
	CPPUNIT_TEST(test_remove);
	CPPUNIT_TEST(test_set);
	CPPUNIT_TEST(test_inherited_add);
	CPPUNIT_TEST(test_inherited_add_all);
	CPPUNIT_TEST(test_inherited_remove);
	
	CPPUNIT_TEST_SUITE_END();

private:
	Gmof::ReflectiveSequence* sequence;
	Gmof::Comment* comment;

public:
	void setUp()
	{
		sequence = new Gmof::ReflectiveSequence();
		comment = new Gmof::Comment("Test comment");
	}

	void tearDown()
	{
		sequence->unreference();
	}

	void test_creation()
	{
		CPPUNIT_ASSERT(sequence);
		
		comment->unreference(); // Prevent memleak
	}
	
	void test_add()
	{
		sequence->add(0, comment);
		
		CPPUNIT_ASSERT(sequence->size() == 1);
	}
	
	void test_get()
	{
		sequence->add(0, comment);
		Gmof::Comment* other = dynamic_cast<Gmof::Comment*>(sequence->get(0));
		
		CPPUNIT_ASSERT(other);
		CPPUNIT_ASSERT(other == comment);
		
		other->unreference();
	}
	
	void test_remove()
	{
		sequence->add(0, comment);
		Gmof::Object* removed = sequence->remove(0);
		removed->unreference();
		
		CPPUNIT_ASSERT(sequence->size() == 0);
	}
	
	void test_set()
	{
		sequence->add(0, comment);
		Gmof::Comment* other = new Gmof::Comment("Another test comment");
		Gmof::Object* replaced = sequence->set(0, other);
		replaced->unreference();
		
		CPPUNIT_ASSERT(sequence->size() == 1);
		
		other = dynamic_cast<Gmof::Comment*>(sequence->get(0));
		
		CPPUNIT_ASSERT(other->body == "Another test comment");
		other->unreference();
	}
	
	void test_inherited_add()
	{
		CPPUNIT_ASSERT(sequence->add(comment) == true);
		
		CPPUNIT_ASSERT(sequence->size() == 1);
	}

	void test_inherited_add_all()
	{
		Gmof::ReflectiveSequence* list = new Gmof::ReflectiveSequence();
		list->add(new Gmof::Comment("Additional comment 1"));
		list->add(new Gmof::Comment("Additional comment 2"));
		
		sequence->add(comment);
		CPPUNIT_ASSERT(sequence->add_all(list) == true);
		
		list->unreference();
		
		CPPUNIT_ASSERT(sequence->size() == 3);
	}
	
	void test_inherited_remove()
	{
		sequence->add(comment);
		CPPUNIT_ASSERT(sequence->remove(comment) == true);
		
		CPPUNIT_ASSERT(sequence->size() == 0);
	}
};

//CPPUNIT_TEST_SUITE_REGISTRATION(ReflectiveSequenceTest);

// End of file
